package com.freedom.ums.member.infrastructure.persistence.repository;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.common.framework.exception.ServiceException;
import com.freedom.ums.member.domain.repository.MemberRepository;
import com.freedom.ums.member.infrastructure.persistence.assembler.MemberAssembler;
import com.freedom.ums.member.infrastructure.persistence.mapper.MemberMapper;
import com.freedom.ums.member.infrastructure.persistence.pojo.bo.MemberBO;
import com.freedom.ums.member.infrastructure.persistence.pojo.po.MemberPO;
import com.freedom.ums.member.infrastructure.persistence.pojo.query.admin.AdminMemberPageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MemberRepositoryImpl implements MemberRepository {

    private final MemberMapper memberMapper;

    private final MemberAssembler memberAssembler;

    @Override
    public Page<MemberBO> queryMemberPage(AdminMemberPageQuery queryParam) {
        Page<MemberPO> page = new Page<>(queryParam.getCurrent(), queryParam.getPageSize());
        Page<MemberPO> memberPOPage = memberMapper.selectMemberPage(page, queryParam);
        return memberAssembler.toPageBO(memberPOPage);
    }

    @Override
    public Boolean modifyStatus(List<Integer> memberIds, Integer status) {
        return memberMapper.batchUpdateStatus(memberIds, status);
    }

    @Override
    public MemberBO findMemberByMobile(String mobile) {
        MemberPO memberPO = memberMapper.selectMemberByMobile(mobile);
        return memberAssembler.toBO(memberPO);
    }

    @Override
    public Integer saveMember(MemberBO memberBO) {
        MemberPO memberPO = memberAssembler.toPO(memberBO);
        Assert.isTrue(memberMapper.insertMember(memberPO), () -> new ServiceException("新增用户失败"));
        return memberPO.getId();
    }
}
